package com.lj.back.manage.scaffold.module.sys.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import com.lj.back.manage.scaffold.common.DTO.PageQuery;
import com.lj.back.manage.scaffold.common.response.R;
import com.lj.back.manage.scaffold.common.util.CommonUtil;
import com.lj.back.manage.scaffold.common.valid.group.AddStrategy;
import com.lj.back.manage.scaffold.common.valid.group.UpdateStrategy;
import com.lj.back.manage.scaffold.module.sys.DTO.SysRoleDTO;
import com.lj.back.manage.scaffold.module.sys.VO.SysRoleVO;
import com.lj.back.manage.scaffold.module.sys.annotation.SysLog;
import com.lj.back.manage.scaffold.module.sys.enums.OperateTypeEnum;
import com.lj.back.manage.scaffold.module.sys.service.SysRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.LinkedHashSet;
import java.util.stream.Collectors;

/**
 * @author lv
 * @version 1.0.0
 */
@RestController
@RequestMapping("/sys/role")
@Api(tags = "后台管理---角色模块")
@ApiSupport(author = "lv")
@AllArgsConstructor
public class SysRoleController {

    private final SysRoleService sysRoleService;

    @GetMapping("/list")
    @ApiOperation(value = "分页获取角色列表")
    @ApiOperationSupport(order = 1)
    @RequiresPermissions("sys:role:list")
    public R listRoles(@Validated PageQuery pageQuery){
        IPage<SysRoleDTO> sysRoleDTOPage =  sysRoleService.listRoles(pageQuery);
        IPage<SysRoleVO> sysRoleVOPage =  CommonUtil.pageBeanConvert(sysRoleDTOPage, SysRoleVO.class);
        return R.success(sysRoleVOPage);
    }

    @GetMapping("/all")
    @ApiOperation(value = "获取所有角色")
    @ApiOperationSupport(order = 2)
    @RequiresPermissions("sys:role:list")
    public R listRoles(){
        LinkedHashSet<SysRoleDTO> sysRoleDTOS =  sysRoleService.listRoles();
        if (CollectionUtil.isEmpty(sysRoleDTOS)){
            return R.success();
        }
        LinkedHashSet<SysRoleVO> sysRoleVOS =  sysRoleDTOS.stream()
                .map(sysRoleDTO -> BeanUtil.copyProperties(sysRoleDTO,SysRoleVO.class))
                .collect(Collectors.toCollection(LinkedHashSet::new));
        return R.success(sysRoleVOS);
    }

    @GetMapping("/info/{id}")
    @ApiOperation(value = "根据ID获取角色详情")
    @ApiOperationSupport(order = 3)
    @RequiresPermissions("sys:role:select")
    @ApiImplicitParam(name = "id", value = "角色ID", example = "1", required = true, dataTypeClass = Long.class)
    public R getRoleInfo(@PathVariable Long id){
        SysRoleDTO sysRoleDTO = sysRoleService.getRoleInfo(id);
        return R.success(sysRoleDTO);
    }

    @PatchMapping("/update")
    @SysLog(value = "更新角色", operateType = OperateTypeEnum.UPDATE)
    @ApiOperation(value = "更新角色")
    @ApiOperationSupport(order = 4,ignoreParameters={"createTime","updateTime","name"})
    @RequiresPermissions("sys:role:update")
    public R updateRole(@RequestBody @Validated(UpdateStrategy.class) SysRoleDTO sysRoleDTO){
        boolean isSuccess = sysRoleService.updateRole(sysRoleDTO);
        return R.build(isSuccess);
    }

    @DeleteMapping("/delete/{id}")
    @SysLog(value = "删除角色", operateType = OperateTypeEnum.DELETE)
    @ApiOperation(value = "删除角色")
    @ApiOperationSupport(order = 5)
    @RequiresPermissions("sys:role:delete")
    @ApiImplicitParam(name = "id", value = "角色ID", example = "1", required = true, dataTypeClass = Long.class)
    public R deleteRole(@PathVariable Long id){
        boolean isSuccess = sysRoleService.removeRole(id);
        return R.build(isSuccess);
    }

    @PostMapping("/add")
    @SysLog(value = "新增角色", operateType = OperateTypeEnum.INSERT)
    @ApiOperation(value = "新增角色")
    @ApiOperationSupport(order = 6,ignoreParameters={"createTime","updateTime","id"})
    @RequiresPermissions("sys:role:insert")
    public R addRole(@RequestBody @Validated(AddStrategy.class) SysRoleDTO sysRoleDTO){
        Long id = sysRoleService.addRole(sysRoleDTO);
        return R.build("id",id);
    }
}
